package com.example.jf.jfapp.fragment.faxian;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.jf.jfapp.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import uk.co.senab.photoview.PhotoView;

/**
 * Created by jf on 2017/7/29.
 */

public class ImageDetailFragment extends android.support.v4.app.Fragment {
    @BindView(R.id.photo_view)
    PhotoView photoView;
    String imageUrl;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        imageUrl = getArguments().getString("url");

    }

    public static ImageDetailFragment getInstance(String imageUrl){
        ImageDetailFragment fragment = new ImageDetailFragment();
        Bundle bundle = new Bundle();
        bundle.putString("url",imageUrl);
        fragment.setArguments(bundle);
        return  fragment;
    }



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View v = inflater.inflate(R.layout.fragment_image_detail,
                container, false);
        ButterKnife.bind(this,v);
        return v;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (imageUrl != null && !"".equals(imageUrl)) {
            //区分gif 和 普通图片
            if (imageUrl.endsWith(".gif")) {
                Glide.with(this)
                        .load(imageUrl)
                        .asGif()
                        .error(R.mipmap.ic_error)
                        .dontAnimate()
                        .diskCacheStrategy(DiskCacheStrategy.SOURCE)// DiskCacheStrategy.NONE
                        .into(photoView);
            } else {
                Glide.with(this)
                        .load(imageUrl)
                        .crossFade()
                        .error(R.mipmap.ic_error)
                        .dontAnimate()
                        .into(photoView);
            }
        }

        photoView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });


    }
}
